<extend name="./Application/Admin/View/Layouts/application.html"/>

<block name="css">
    <link rel="stylesheet" href="__PUBLIC__/assets/admin/common.css">
</block>

<block name="content">
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">商品列表</strong> /
                    <small>Product List</small>
                </div>
            </div>

            <hr>

            <div class="am-g">
                <div class="am-u-sm-12 am-u-md-6">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="{{:U('Product/add')}}" class="am-btn am-btn-default"><span
                                    class="am-icon-plus"></span> 新增
                            </a>
                            <button class="recycle_all am-btn am-btn-default"><span class="am-icon-recycle"></span> 回收
                            </button>
                        </div>
                    </div>
                </div>

                <form>
                    <div class="am-u-sm-12 am-u-md-3">
                        <div class="am-form-group">
                            <select name="category_id"
                                    data-am-selected="{btnWidth: '100%',  btnStyle: 'secondary', btnSize: 'sm', maxHeight: 260, searchBox: 1}">
                                <volist name="categories" id="category">
                                    <option value="{{$category.id}}" disabled>{{$category.name}}</option>
                                    <volist name="category.children" id="child">
                                        <option value="{{$child.id}}">
                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{$child.name}}
                                        </option>
                                    </volist>
                                </volist>
                            </select>
                        </div>
                    </div>
                    <div class="am-u-sm-12 am-u-md-3">
                        <div class="am-input-group am-input-group-sm">
                            <input type="text" name="name" class="am-form-field">
                            <span class="am-input-group-btn">
                                <button class="am-btn am-btn-default" type="submit">搜索</button>
                                <a href="{{:U('Product/index')}}" class="am-btn am-btn-default">重置</a>
                            </span>
                        </div>
                    </div>
                </form>

            </div>

            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr>
                                <th>
                                    <input type="checkbox" class="check_all"/>
                                </th>
                                <th>ID</th>
                                <th>名称</th>
                                <th>类名</th>
                                <th>缩略图</th>
                                <th>价格</th>
                                <th>库存</th>
                                <th>置顶</th>
                                <th>新品</th>
                                <th>热卖</th>
                                <th>推荐</th>
                                <th>上架</th>
                                <th>排序</th>
                                <th>发布时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>

                            <volist name="products" id="product">
                                <tr data-id="{{$product.id}}">
                                    <td>
                                        <input type="checkbox" name="ids[]" value="{{$product.id}}" class="checked_id"/>
                                    </td>
                                    <td>{{$product.id}}</td>
                                    <td>{{$product.name}}</td>
                                    <td>{{$product.category.name}}</td>
                                    <td>
                                        <img class="am-circle" src="{{$product.thumb}}" style="width: 50px;height:50px;"
                                             alt="">
                                    </td>
                                    <td>{{$product.price}}</td>
                                    <td>
                                        <input type="hidden" name="id[]" value="{{$product.id}}">
                                        <input type="text" class="sort_stock" name="stock[]" value="{{$product.stock}}"
                                               style="width:60px;">
                                    </td>

                                    <foreach name="datas" item="data">
                                        <td>
                                            {{:is_something($product, $data)}}
                                        </td>
                                    </foreach>

                                    <td>
                                        <input type="hidden" name="id[]" value="{{$product.id}}">
                                        <input type="text" name="sort_order[]" class="sort_order"
                                               value="{{$product.sort_order}}" style="width:40px;">
                                    </td>
                                    <td>{{$product.created_at|date='Y-m-d',###}}</td>
                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <a href="{{:U('edit',['id'=>$product[id]])}}"
                                                   class="am-btn am-btn-default am-btn-xs am-text-secondary"><span
                                                        class="am-icon-pencil-square-o"></span> 编辑
                                                </a>

                                                <button type="button"
                                                        class="recycle_product am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                    <span class="am-icon-recycle"></span> 回收
                                                </button>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            </volist>
                            </tbody>
                        </table>
                        <div class="am-cf">
                            共 {{$count}} 条记录
                            <div class="am-fr">
                                <ul class="am-pagination">

                                    {{$pages}}

                                </ul>
                            </div>
                        </div>
                        <hr/>
                    </form>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <hr>
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>

    </div>

    <!--ajax回收单个-->
    <div class="am-modal am-modal-confirm" tabindex="-1" id="recycleProduct">
        <div class="am-modal-dialog">

            <input type="hidden" id="recycle_id">
            <div class="am-modal-hd">回收单个商品</div>
            <div class="am-modal-bd">
                你，确定要回收这个商品吗？
            </div>
            <div class="am-modal-footer">
                <span class="am-modal-btn" data-am-modal-cancel>取消</span>
                <span class="recycle_confirm am-modal-btn" data-am-modal-confirm>确定</span>
            </div>
        </div>
    </div>


</block>
<block name="js">
    <script>
        $(function () {
            //弹出删除模态框
            $('#doc-modal-list').find('.am-icon-close').add('.recycle_product').on('click', function () {
                $('#recycleProduct').modal();
                var id = $(this).parents('tr').data('id');
                $('#recycle_id').val(id);
            });
            $('.recycle_confirm').click(function () {
                var id = $('#recycle_id').val();

                $.post("{{:U('ajax_recycle')}}", {id: id}, function () {
                    $("tr[data-id='" + id + "']").fadeOut(400);
                    $('#recycleProduct').modal().hide();
                })
                return false;
            })

            //全选和不全选
            $(".check_all").click(function () {
                var checked = $(this).prop('checked');
                $('.checked_id').prop('checked', checked);
            })
            //下面全选上面也选中
            $('.checked_id').click(function () {
                var allLength = $(".checked_id").length;
                var check_length = $(".checked_id:checked").length;
                if (allLength == check_length) {
                    $(".check_all").prop('checked', true);
                } else {
                    $(".check_all").prop('checked', false)
                }
            })

            //ajax多选回收
            $(".recycle_all").click(function () {
                var length = $(".checked_id:checked").length;
                if (length == 0) {
                    alert('请你至少选择一条');
                    return false;
                }

                $.post("{{:U('ajax_recycle_all')}}", $('.checked_id:checked').serialize(), function () {
                    $('.checked_id:checked').each(function () {
                        $(this).parents('tr').fadeOut(400);
                    })
                })
                return false;
            })

            //ajax排序
            $(".sort_order").change(function () {
                var info = {
                    id: $(this).parents('tr').data('id'),
                    sort_order: $(this).val()
                }
                $.post("{{:U('ajax_sort_order')}}", info, function () {
                    window.location.reload();
                })
                return false;
            })

            //ajax改变属性
            $(".change_attr").click(function () {
                var info = {
                    id: $(this).parents('tr').data('id'),
                    attr: $(this).attr('data-attr')
                }

                var _this = $(this);
                $.post("{{:U('change_attr')}}", info, function () {
                    _this.toggleClass('am-icon-check am-icon-close');
                })
                return false;
            })

        })


    </script>
</block>